package com.javaspeak.designpatterns.go4.behavioural.iterator;


public class LinkedElement<E> implements Cloneable {

    // This is not populated by add or remove method. It is used for post
    // processing such as when creating an SnapshotIterator
    LinkedElement<E> previousLinkedElement;

    // In ConcurrentLinkedList there is a an AtomicReferenceFieldUpdater which
    // is used to update this volatile field using CAS
    volatile LinkedElement<E> nextLinkedElement;

    // The object
    E object;


    /**
     * Constructor
     *
     * @param object
     */
    LinkedElement( E object ){

        this.object = object;
    }


    /* (non-Javadoc)
     * @see java.lang.Object#clone()
     */
    @Override
    protected LinkedElement<E> clone() throws CloneNotSupportedException {

        // This method is used to clone a chain of LinkedElement to create a
        // Snapshot Iterator

        LinkedElement<E> clonedLinkedElement = new LinkedElement<E>( object );

        if ( nextLinkedElement != null ){

            clonedLinkedElement.nextLinkedElement = nextLinkedElement.clone();
        }

        return clonedLinkedElement;
    }
}
